package com.starlight.flower_server.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.starlight.flower_server.controller.authentication.AuthenticationCheck;
import com.starlight.flower_server.controller.exception.BadRequestException;
import com.starlight.flower_server.domain.TotalData;
import com.starlight.flower_server.domain.flowerUser.cqrs.*;
import com.starlight.flower_server.domain.flowerUser.data.FlowerUserData;
import com.starlight.flower_server.domain.service.LoginInfoService;
import com.starlight.flower_server.domain.service.captcha.CaptchaService;
import com.starlight.flower_server.infrastructure.FlowerServerResponse;
import com.starlight.flower_server.infrastructure.RoleType;
import com.starlight.flower_server.infrastructure.Status;
import com.starlight.flower_server.infrastructure.entity.FlowerUser;
import com.starlight.flower_server.domain.service.FlowerUserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * @Author yfy2001
 * @date 2023/4/5 15:04
 * 用户接口
 */
@RestController
@RequestMapping("/flowerUser")
public class FlowerUserController {
    @Resource
    private FlowerUserService flowerUserService;

    @Resource
    private CaptchaService captchaService;

    @Resource
    private LoginInfoService loginInfoService;


    /**
     * 后台直接创建用户
     * @param flowerUser
     * @return
     */
    @PostMapping("/createFlowerUser")
    public FlowerServerResponse<Integer> createFlowerUser(@RequestBody FlowerUser flowerUser){
        return FlowerServerResponse.success(flowerUserService.insertUser(flowerUser));
    }

    /**
     * 前台用户注册
     * @param command
     * String username;
     * String email;
     * String password;
     * String nickname;
     * String captcha;
     * @return
     * Integer是否成功信息
     */
    @PostMapping("/flowerUserRegister")
    public FlowerServerResponse<Integer> flowerUserRegister(@RequestBody FlowerUserRegisterCommand command){
       if (captchaService.verifyCaptcha(command.getEmail(), command.getCaptcha())){
           FlowerUser flowerUser=new FlowerUser();
           flowerUser.setUsername(command.getUsername());
           flowerUser.setEmail(command.getEmail());
           flowerUser.setPassword(command.getPassword());
           flowerUser.setNickname(command.getNickname());
           return FlowerServerResponse.success(flowerUserService.insertUser(flowerUser));
       }else {
           throw new BadRequestException(Status.CAPTCHA_ERROR.getMessage(),Status.CAPTCHA_ERROR);
       }
    }

    /**
     * 用户重置密码
     * @param command
     * String email;
     * String password;
     * String captcha;
     * @return
     * Integer是否成功信息
     */
    @PostMapping("/flowerUserResetPassword")
    public FlowerServerResponse<Integer> flowerUserResetPassword(@RequestBody FlowerUserResetPasswordCommand command){
        System.out.println(command);
        if (captchaService.verifyCaptcha(command.getEmail(), command.getCaptcha())){
            FlowerUser flowerUser=new FlowerUser();
            flowerUser.setId(command.getId());
            flowerUser.setPassword(command.getPassword());
            return FlowerServerResponse.success(flowerUserService.resetPassword(flowerUser));
        }else {
            throw new BadRequestException(Status.CAPTCHA_ERROR.getMessage(),Status.CAPTCHA_ERROR);
        }
    }

    /**
     * 用户修改邮箱
     * @param command
     * @return
     */
    @PostMapping("/flowerUserResetEmail")
    @AuthenticationCheck(roleType = {RoleType.ROOT,RoleType.NORMAL,RoleType.PRIMARY,RoleType.SENIOR})
    public FlowerServerResponse<Integer> flowerUserResetEmail(@RequestBody FlowerResetEmailCommand command){
        System.out.println(command);
        if (captchaService.verifyCaptcha(command.getEmail(), command.getCaptcha())){
            FlowerUser flowerUser=new FlowerUser();
            flowerUser.setId(command.getId());
            flowerUser.setPassword(command.getEmail());
            return FlowerServerResponse.success(flowerUserService.resetEmail(flowerUser));
        }else {
            throw new BadRequestException(Status.CAPTCHA_ERROR.getMessage(),Status.CAPTCHA_ERROR);
        }
    }

    /**
     * 用户存在判断
     * @param command
     * String username;
     * String email;
     * @return
     * 存在返回id
     * 不存在返回0
     */
    @PostMapping("/flowerUserExistsTest")
    public FlowerServerResponse<Long> flowerUserExistsTest(@RequestBody FlowerUserExistsTestCommand command){
        FlowerUser flowerUser=new FlowerUser();
        flowerUser.setUsername(command.getUsername());
        flowerUser.setEmail(command.getEmail());
        return FlowerServerResponse.success(flowerUserService.userExists(flowerUser));
    }

    @GetMapping("/getFlowerUserDetailById")
    public FlowerServerResponse<FlowerUser> getFlowerUserDetailById(Long id){
        return FlowerServerResponse.success(flowerUserService.queryUserDetailById(id));
    }

    @PostMapping("/deleteFlowerUserById")
    public FlowerServerResponse<Integer> deleteFlowerUserById(Long id){
        return FlowerServerResponse.success(flowerUserService.logicDeleteUserById(id));
    }

    @PostMapping("/updateFlowerUser")
    public FlowerServerResponse<Integer> updateFlowerUser(@RequestBody FlowerUser flowerUser){
        return FlowerServerResponse.success(flowerUserService.modifyUser(flowerUser));
    }

    @GetMapping("/getFlowerUserPage")
    public FlowerServerResponse<IPage<FlowerUserData>> getFlowerUserPage(FlowerUserPageQuery query){
        return FlowerServerResponse.success(flowerUserService.queryUserList(query));
    }

    @GetMapping("/getAllFlowerUserData")
    public FlowerServerResponse<TotalData<FlowerUserData>> getAllFlowerUserData(){
        return FlowerServerResponse.success(flowerUserService.queryAllFlowerUserData());
    }

    @GetMapping("/getFlowerUserTotalCount")
    public FlowerServerResponse<TotalData<FlowerUserData>> getFlowerUserTotalCount(){
        return FlowerServerResponse.success(flowerUserService.queryFlowerUserTotalCount());
    }

}
